package com.road.controller;

import com.google.common.collect.Maps;
import com.road.model.easyui.PageInfo;
import com.road.model.easyui.Tree;
import com.road.model.entity.Role;
import com.road.service.RoleService;
import com.road.utils.MessageModel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * @description：权限管理
 */
@Controller
@RequestMapping("/system/role")
public class RoleController extends BaseController {

	private static Logger logger = LoggerFactory.getLogger(RoleController.class);

	@Autowired
	private RoleService roleService;

	/**
	 * 
	 * @Name: manager
	 * @Description:角色管理默认页
	 * @Author: WeiHui.Zhang
	 * @Version: V1.00
	 * @Date: 2016年3月23日下午7:42:44
	 * @return
	 * @Return: String
	 */
	@RequestMapping(method = RequestMethod.GET)
	public String manager() {
		return "system/roleList";
	}

	/**
	 * 角色列表
	 *
	 * @param page
	 * @param rows
	 * @param sort
	 * @param order
	 * @return
	 */
	@RequestMapping(value = "/roleList", method = RequestMethod.POST)
	@ResponseBody
	public PageInfo<Role> roleList(Integer page, Integer rows, String sort, String order) {
		PageInfo<Role> pageInfo = new PageInfo<Role>(page, rows, sort, order);
		Map<String, Object> condition = Maps.newHashMap();
		pageInfo.setCondition(condition);
		roleService.findDataGrid(pageInfo);
		return pageInfo;
	}

	/**
	 * 
	 * @Name: roleStatus
	 * @Description:用户启用禁用
	 * @Author: WeiHui.Zhang
	 * @Version: V1.00
	 * @Date: 2016年4月1日下午5:48:53
	 * @param id
	 * @param status
	 * @return
	 * @Return: String
	 */
	@RequestMapping(value = "/status/{id}/{status}", method = RequestMethod.POST)
	@ResponseBody
	public String roleStatus(@PathVariable(value = "id") String id, @PathVariable(value = "status") String status) {
		int roleId = Integer.parseInt(id);
		int open = Integer.parseInt(status);
		roleService.updateRoleStatus(roleId, open);
		// 更新角色 启用、禁用等
		return Boolean.TRUE.toString();
	}

	/**
	 * 权限树
	 */
	@RequestMapping(value = "/tree", method = RequestMethod.POST)
	@ResponseBody
	public List<Tree> tree() {
		return roleService.findTree();
	}

	/**
	 * 添加角色页
	 */
	@RequestMapping(value = "/create", method = RequestMethod.GET)
	public String addRole(Model model) {
		model.addAttribute("role", new Role());
		model.addAttribute("action", "create");
		return "system/roleForm";
	}

	/**
	 * 添加角色
	 *
	 * @param role
	 * @return
	 */
	@RequestMapping(value = "add", method = RequestMethod.POST)
	@ResponseBody
	public String add(Role role) {
		roleService.addRole(role);
		return Boolean.TRUE.toString();
	}

	/**
	 * 
	 * @Name: update
	 * @Description:角色更新页
	 * @Author: WeiHui.Zhang
	 * @Version: V1.00
	 * @Date: 2016年4月1日下午5:51:20
	 * @param id
	 * @param model
	 * @return
	 * @Return: String
	 */
	@RequestMapping(value = "update/{id}", method = RequestMethod.GET)
	public String update(@PathVariable("id") Long id, Model model) {
		model.addAttribute("role", roleService.findRoleById(id));
		model.addAttribute("action", "update");
		return "system/roleForm";
	}

	/**
	 * 删除权限
	 *
	 * @param id
	 * @return
	 */
	@RequestMapping("/delete")
	@ResponseBody
	public MessageModel delete(Long id) {
		MessageModel result = new MessageModel();
		try {
			roleService.deleteRoleById(id);
			result.setMsg("删除成功！");
			result.setSuccess(true);
			return result;
		} catch (RuntimeException e) {
			logger.error("删除角色失败：{}", e);
			result.setMsg(e.getMessage());
			return result;
		}
	}

	/**
	 * 删除权限
	 *
	 * @param role
	 * @return
	 */
	@RequestMapping("/edit")
	@ResponseBody
	public MessageModel edit(Role role) {
		MessageModel result = new MessageModel();
		try {
			roleService.updateRole(role);
			result.setSuccess(true);
			result.setMsg("编辑成功！");
			return result;
		} catch (RuntimeException e) {
			logger.error("编辑角色失败：{}", e);
			result.setMsg(e.getMessage());
			return result;
		}
	}

	/**
	 * 授权页面
	 *
	 * @param request
	 * @param id
	 * @param model
	 * @return
	 */
	@RequestMapping("/grantPage")
	public String grantPage(HttpServletRequest request, Long id, Model model) {
		model.addAttribute("id", id);
		return "/admin/roleGrant";
	}

	/**
	 * 授权页面页面根据角色查询资源
	 *
	 * @param request
	 * @param id
	 * @param model
	 * @return
	 */
	@RequestMapping("/findResourceIdListByRoleId")
	@ResponseBody
	public MessageModel findResourceByRoleId(HttpServletRequest request, Long id, Model model) {
		MessageModel result = new MessageModel();
		try {
			List<Long> resources = roleService.findResourceIdListByRoleId(id);
			result.setSuccess(true);
			result.setObj(resources);
			return result;
		} catch (RuntimeException e) {
			logger.error("角色查询资源失败：{}", e);
			result.setMsg(e.getMessage());
			return result;
		}
	}

	/**
	 * 授权
	 *
	 * @param id
	 * @param resourceIds
	 * @return
	 */
	@RequestMapping("/grant")
	@ResponseBody
	public MessageModel grant(Long id, String resourceIds) {
		MessageModel result = new MessageModel();
		try {
			roleService.updateRoleResource(id, resourceIds);
			result.setMsg("授权成功！");
			result.setSuccess(true);
			return result;
		} catch (RuntimeException e) {
			logger.error("授权成功失败：{}", e);
			result.setMsg(e.getMessage());
			return result;
		}
	}

}
